package Demo1;

public class Demo1 {
    public static void main1(String[] args) throws InterruptedException {
        Thread t=new Thread(()->{
            for (int i = 0; i <3 ; i++) {

                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("thread线程 结束");
        });
        System.out.println(t.getState());
        t.start();
        System.out.println(t.getState());
        t.join(4000);
        System.out.println("main");
    }


    public static void main2(String[] args) throws InterruptedException {
        Thread t=new Thread(()->{
           while(true){
               System.out.println("hello thread");
               try {
                   Thread.sleep(2000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }

        });
t.start();
        Thread.sleep(1000);
        System.out.println(t.getState());
    }

    private static int count=0;

    public static void main3(String[] args) throws InterruptedException {
        Thread t1=new Thread(()->{
            for (int i = 0; i <5000 ; i++) {
                count++;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        Thread t2=new Thread(()->{
            for (int i = 0; i <5000 ; i++) {
                count++;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        t1.start();
        t1.join();
        t2.start();
        t2.join();
        System.out.println(count);
    }


    public static void main4(String[] args) throws InterruptedException {
        Thread M=Thread.currentThread();

Thread t=new Thread(()->{

    try {
        System.out.println("t线程开始");
        M.join();
        System.out.println("t线程结束");
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
});
t.start();
Thread.sleep(1000);
        System.out.println("main线程结束");
    }

    public static void main(String[] args) {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("1");
            }
        });
        t.start();
        System.out.println("2");
    }


}
